import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:ducafe_ui_core/ducafe_ui_core.dart';
import 'dart:async';

import 'package:hinge/common/index.dart';

class SplashPage extends StatefulWidget {
  const SplashPage({super.key});

  @override
  State<SplashPage> createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage>
    with SingleTickerProviderStateMixin {
  static const String splash = 'assets/images/splash_bg.png';
  late AnimationController _animationController;
  late Animation<double> _fadeAnimation;

  @override
  void initState() {
    super.initState();

    // 创建动画控制器
    _animationController = AnimationController(
      duration: const Duration(milliseconds: 1500),
      vsync: this,
    );

    // 创建淡入动画
    _fadeAnimation = Tween<double>(begin: 0.0, end: 1.0).animate(
      CurvedAnimation(parent: _animationController, curve: Curves.easeIn),
    );

    // 启动动画
    _animationController.forward();

    // 执行游客自动登录并跳转
    _autoLoginAndNavigate();
  }

  // 游客自动登录并导航
  Future<void> _autoLoginAndNavigate() async {
    // 延迟2秒，确保动画有足够时间展示
    await Future.delayed(const Duration(seconds: 2));

    // 执行游客自动登录
    await UserService.autoGuestLogin();

    // 登录完成后导航到主页
    if (mounted) {
      context.go('/');
    }
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        width: ScreenUtil().screenWidth,
        height: ScreenUtil().screenHeight,
        decoration: const BoxDecoration(
          image: DecorationImage(image: AssetImage(splash), fit: BoxFit.cover),
        ),
        child: FadeTransition(
          opacity: _fadeAnimation,
          child: SafeArea(
            child: Stack(
              children: [
                // 顶部标题
                Positioned(
                  top: 60.h,
                  left: 0,
                  right: 0,
                  child: Center(
                    child: Text(
                      'Welcome to Hinge',
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 20.sp,
                        fontWeight: FontWeight.w600,
                        letterSpacing: 1.2,
                        shadows: [
                          Shadow(
                            blurRadius: 10.0,
                            color: Colors.black.withOpacity(0.3),
                            offset: const Offset(0, 2),
                          ),
                        ],
                      ),
                    ),
                  ),
                ),

                // 中央Logo和App名称 - 使用Positioned和Center确保居中
                Positioned.fill(
                  child: Center(
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Image.asset(
                          AssetsImages.icon,
                          width: 150.w,
                          height: 150.w,
                        ),
                        SizedBox(height: 30.h),
                        Text(
                          'Hinge',
                          style: TextStyle(
                            color: Colors.black,
                            fontSize: 30.sp,
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
